package com.lagou.edu.controller;

import com.lagou.edu.service.CodeServiceFeignClient;
import com.lagou.edu.service.TokenService;
import com.lagou.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("api/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private CodeServiceFeignClient codeServiceFeignClient;

    /**
     * 注册接⼝，true成功，false失败
     *
     * @return
     */
    @RequestMapping("/register/{email}/{password}/{code}")
    public String register(HttpServletResponse response,
                           @PathVariable String email,
                           @PathVariable String password,
                           @PathVariable String code) {
        int validate = codeServiceFeignClient.validate(email, code);
        if (0 == validate) {
            if (userService.register(response, email, password)) {
                return "0";
            }
        } else if (1 == validate) {
            return "验证码校验失败";
        } else if (2 == validate) {
            return "验证码失效";
        }
        return "未知异常";
    }

    /**
     * 是否已注册，根据邮箱判断,true代表已经注册过，false代表尚未注册
     *
     * @return
     */
    @RequestMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable String email) {
        return userService.isRegistered(email);
    }

    /**
     * 登录接⼝，验证⽤户名密码合法性，根据⽤户名和
     * 密码⽣成token，token存⼊数据库，并写⼊cookie
     * 中，登录成功返回邮箱地址，重定向到欢迎⻚
     *
     * @return 邮箱地址
     */
    @RequestMapping("/login/{email}/{password}")
    public String login(HttpServletResponse response,
                        @PathVariable String email,
                        @PathVariable String password) throws IOException {
        if (userService.login(response, email, password)) {
            return email;
        }
        throw new RuntimeException("登录失败");
    }

    /**
     * 根据token查询⽤户登录邮箱接⼝
     *
     * @return 邮箱地址
     */
    @RequestMapping("/info/{token}")
    public String info(@PathVariable String token) {
        return tokenService.info(token);
    }

    /**
     * 根据token查询⽤户登录邮箱接⼝
     *
     * @return 邮箱地址
     */
    @RequestMapping("/info")
    public String infoByCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return "";
        }
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if ("wxs".equals(cookie.getName())) {
                return tokenService.info(cookie.getValue());
            }
        }
        return "";
    }
}
